import PyDGGS

# 本example主要是用来展示如何使用DGGSField库实现对栅格类型数据的管理和组织
# 本example只针对六边形格元


def create_field_layer(band_count, data_type, element_type, grid_type, level, bound):
    """
    初始化指定范围图层
    :param band_count: 波段数
    :param data_type: 数据类型
    :param element_type: 要素类型
    :param grid_type: 格网类型
    :param level: 层级
    :param bound: 边界范围
    :return: 一个field的图层对象
    """
    if data_type == PyDGGS.DGGS_Int16:
        field_layer = PyDGGS.DGGSFieldLayerShort(band_count, data_type, element_type, grid_type, level, bound)
        return field_layer
    elif data_type == PyDGGS.DGGS_Int32:
        field_layer = PyDGGS.DGGSFieldLayerInt(band_count, data_type, element_type, grid_type, level, bound)
        return field_layer
    elif data_type == PyDGGS.DGGS_Float32:
        field_layer = PyDGGS.DGGSFieldLayerFloat(band_count, data_type, element_type, grid_type, level, bound)
        return field_layer
    elif data_type == PyDGGS.DGGS_Float64:
        field_layer = PyDGGS.DGGSFieldLayerDouble(band_count, data_type, element_type, grid_type, level, bound)
        return field_layer


def update_value(field_layer, location, value, band_id):
    """
    修改指定波段指定位置的数据
    :param field_layer: 原field的图层对象
    :param location: 位置（有格网编码和经纬度两种类型）
    :param value: 新的属性值
    :param band_id: 波段索引
    :return: 更新后的图层对象
    """
    field_layer.setValue(location, value, band_id)
    return field_layer


def find_value(field_layer, location, band_id):
    """
    查找制定波段指定位置的属性值
    :param field_layer: field的图层
    :param location: 位置（有格网编码和经纬度两种类型）
    :param band_id: 波段索引
    :return: 该位置的属性值
    """
    return field_layer.getValue(location, band_id);


def main():
    band_count = 1
    data_type = PyDGGS.DGGS_Float64
    element_type = PyDGGS.DGGSElementType_Cell
    grid_type = PyDGGS.DGGSGridType_DGGS_ISEA4H
    level = 5
    bound = PyDGGS.new_array(4)
    # 经纬度的边界四至（-180~180，-90~90）
    PyDGGS.set_item(bound, 0, -180.0)   #上
    PyDGGS.set_item(bound, 1, 180.0)    #下
    PyDGGS.set_item(bound, 2, 89.99)    #左
    PyDGGS.set_item(bound, 3, -89.99)   #右

    # 创建图层
    field_layer = create_field_layer(band_count, data_type, element_type, grid_type, level, bound)
    print("field图层创建完成")

    # 更新属性
    code = PyDGGS.DGGSElementCode()
    code.elementType = element_type
    code.level = level
    code.basePartition = 1
    code.col = 0
    code.row = 2
    value = 11
    field_layer = update_value(field_layer, code, value, 0)
    print(f"属性更新完成为：{value}")

    # 查找属性
    new_value = find_value(field_layer, code, 0)
    print(f"属性查找完成为{new_value}")


if __name__ == "__main__":
    main()

